<?php
/**
 * Created by PhpStorm.
 * User: Monkey
 * Date: 2021/10/13
 * Time: 11:27
 * Desc: 银行卡
 */
declare (strict_types=1);

namespace app\backend\controller;

use app\backend\base\controller\Base;
use app\backend\validate\BankCardValidate;

class Bankcard extends Base
{
    /**
     * 列表
     */
    public function index()
    {
        if (request()->isPost()) {
            $validate = new BankCardValidate();
            $ret = $validate->scene('index')->check(input());
            if (!$ret) {
                return json(_E($validate->getError()));
            }

            $page = input('page/d', 1);
            $limit = input('limit/d', 20);
            $fieldName = input('field_name','');
            $fieldValue = input('field_value','');

            //条件
            $where = [];

            //字段筛选
            if (!empty($fieldName) && !empty($fieldValue)) {
                switch ($fieldName) {
                    case 'account'://账号
                        $userId = \app\extend\model\User::where([
                            ['account','=',$fieldValue]
                        ])->value('id');
                        $where[] = ['uid', '=', $userId];
                        break;
                    case 'uid'://用户ID
                        $where[] = ['uid', '=', $fieldValue];
                        break;
                }
            }

            $list = [];
            $count = \app\extend\model\BankCard::where($where)->count();
            if ($count > 0) {
                $list = \app\extend\model\BankCard::where($where)->append(['user_account_txt'])->page($page)->limit($limit)->select();
            }

            return json([
                'code' => 0,
                'msg' => '获取成功',
                'count' => $count,
                'data' => $list,
            ])->send();
        } else {
            return view('index');
        }
    }

    /**
     * 修改
     */
    public function edit()
    {
        if (request()->isPost()) {
            $validate = new BankCardValidate();
            $ret = $validate->scene('edit')->check(input());
            if (!$ret) {
                return json(_E($validate->getError()));
            }

            $id = input('id/d', 0);
            $username = input('username', '');
            $number = input('number', '');
            $bankName = input('bank_name', '');
            $bankBranchName = input('bank_branch_name', '');

            $result = \app\extend\model\BankCard::where([
                ['id', '=', $id]
            ])->update([
                'username' => $username,
                'number' => $number,
                'bank_name' => $bankName,
                'bank_branch_name' => $bankBranchName,
                'updated_at' => date('Y-m-d H:i:s')
            ]);

            if (!$result) {
                return json(_E('修改失败'));
            } else {
                return json(_S('修改成功'));
            }
        }
    }

    /**
     * 删除
     */
    public function del()
    {
        if (request()->isPost()) {
            $validate = new BankCardValidate();
            $ret = $validate->scene('del')->check(input());
            if (!$ret) {
                return json(_E($validate->getError()));
            }
            $id = input('id/d', 0);

            $result = \app\extend\model\BankCard::where([
                ['id', '=', $id]
            ])->delete();
            if (!$result) {
                return json(_E('删除失败'));
            } else {
                return json(_S('删除成功'));
            }
        }
    }
}
